﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Arrow : MonoBehaviour
{
    public Vector2 StartPoint;
    private Vector2 EndPoint;
    private RectTransform arrow;

    private float ArrowLength;
    private float ArrowTheta;
    private Vector2 ArrowPositon;
    // Start is called before the first frame update
    void Start()
    {
        arrow = transform.GetComponent<RectTransform>();
    }

    // Update is called once per frame
    void Update()
    {
        //获得鼠标位置（因为以游戏坐标为主即（1920 * 1080）
        EndPoint = Input.mousePosition - new Vector3(Screen.width / 2, Screen.height / 2, 0.0f);//计算中点
        ArrowPositon = new Vector2((EndPoint.x + StartPoint.x) / 2, (EndPoint.y + StartPoint.y) / 2);
        ArrowLength = Mathf.Sqrt((EndPoint.x - StartPoint.x) * (EndPoint.x - StartPoint.x) + (EndPoint.y - StartPoint.y) * (EndPoint.y - StartPoint.y)) - 25;
        ArrowTheta = Mathf.Atan2(EndPoint.y - StartPoint.y, EndPoint.x - StartPoint.x);
        //箭头跟着鼠标,并赋值
        arrow.localPosition = ArrowPositon;
        arrow.sizeDelta = new Vector2(ArrowLength, arrow.sizeDelta.y);
        arrow.localEulerAngles = new Vector3(0.0f, 0.0f, ArrowTheta * 180 / Mathf.PI);
    }
    public void SetStartPoint(Vector2 _startPoint)
    {
        StartPoint = _startPoint - new Vector2(960.0f, 540.0f);
    }
}
